
// const CryptoJS = require("./crypto-js");
import * as CryptoJS from "crypto-js";

// 十六位十六进制数作为密钥
const SECRET_KEY = CryptoJS.enc.Utf8.parse("1111111111111111");
// 十六位十六进制数作为密钥偏移量
const SECRET_IV = CryptoJS.enc.Utf8.parse("2222222222222222");

export function encrypt(data) {
  if (typeof data === "object") {
    try {
      // eslint-disable-next-line no-param-reassign
      data = JSON.stringify(data);
    } catch (error) {
      console.log("encrypt error:", error);
    }
  }
  const dataHex = CryptoJS.enc.Utf8.parse(data);
  const encrypted = CryptoJS.AES.encrypt(dataHex, SECRET_KEY, {
    iv: SECRET_IV,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.ciphertext.toString();
}

export function decrypt(word){
  let key = CryptoJS.enc.Utf8.parse("1111111111111111");
  let iv  = CryptoJS.enc.Utf8.parse('2222222222222222');
  let srcs = CryptoJS.enc.Hex.parse(word);
  let m64str=CryptoJS.enc.Base64.stringify(srcs);
  let decrypt = CryptoJS.AES.decrypt(m64str, key, { iv: iv,mode:CryptoJS.mode.CBC});
  return decrypt.toString(CryptoJS.enc.Utf8);
}
